# Unified Makefile for train data monitoring system
# 支持跨平台编译和多个模块

# 编译器设置
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -g
INCLUDES = -I./header

# 检测操作系统
UNAME_S := $(shell uname -s)

# 根据操作系统设置特定的编译标志
ifeq ($(UNAME_S),Darwin)
    # macOS specific flags
    CFLAGS += -D_DARWIN_C_SOURCE
    LIBS = 
else ifeq ($(UNAME_S),Linux)
    # Linux specific flags
    CFLAGS += -D_GNU_SOURCE
    LIBS = -lrt -lpthread
else ifeq ($(OS),Windows_NT)
    # Windows specific flags (if using MinGW or similar)
    CFLAGS += -D_WIN32_WINNT=0x0601
    LIBS = -lws2_32
    # Add hiredis support for Windows via vcpkg (x64 version)
    VCPKG_ROOT = D:/vcpkg/vcpkg
    INCLUDES += -I$(VCPKG_ROOT)/installed/x64-windows/include
    LIBS += -L$(VCPKG_ROOT)/installed/x64-windows/lib -lhiredis
    # hiredis is x64 version, so ensure you are building for x64
    # Add debugging and compatibility flags
    CFLAGS += -D_CRT_SECURE_NO_WARNINGS
    LIBS += -lws2_32 -liphlpapi
else
    # Default flags for other systems
    LIBS = -lrt -lpthread
endif

# 目标文件定义
CONTROL_TARGET = control_data


GET_DATA_TARGET = get_data_mac
GET_DATA_SOURCE = get_data_mac.c

REDIS_TARGET = rediscache_test
REDIS_SOURCE = lib/rediscache.c

REDIS_SIMPLE_TARGET = rediscache_simple
REDIS_SIMPLE_SOURCE = lib/rediscache_simple.c

# 默认目标 - 编译所有模块
all: $(CONTROL_TARGET) $(GET_DATA_TARGET) $(REDIS_TARGET)

# 编译control_data模块
$(CONTROL_TARGET): $(CONTROL_OBJS)
	$(CC) $(CFLAGS) -o $@ $^ $(LIBS)

# 编译control_data.o
control_data.o: control_data.c  lib/time_period.h
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# 编译sharemem.o
lib/sharemem.o: lib/sharemem.c header/sharemem.h
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# 编译time_period.o
lib/time_period.o: lib/time_period.c lib/time_period.h
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# 编译get_data_mac模块
$(GET_DATA_TARGET): $(GET_DATA_SOURCE)
	$(CC) $(CFLAGS) -o $@ $< $(LIBS)

# 编译rediscache测试模块
$(REDIS_TARGET): $(REDIS_SOURCE)
	$(CC) $(CFLAGS) $(INCLUDES) -o $@ $< $(LIBS)

# 编译简单的rediscache模块（不需要hiredis）
$(REDIS_SIMPLE_TARGET): $(REDIS_SIMPLE_SOURCE)
	$(CC) $(CFLAGS) $(INCLUDES) -o $@ $< $(LIBS)

# 清理目标
clean:
	rm -f $(CONTROL_OBJS) $(CONTROL_TARGET) $(GET_DATA_TARGET) $(REDIS_TARGET) $(REDIS_SIMPLE_TARGET) *.o

# 安装目标（可选）
install: $(CONTROL_TARGET) $(GET_DATA_TARGET) $(REDIS_TARGET)
	cp $(CONTROL_TARGET) /usr/local/bin/
	cp $(GET_DATA_TARGET) /usr/local/bin/
	cp $(REDIS_TARGET) /usr/local/bin/

# 卸载目标（可选）
uninstall:
	rm -f /usr/local/bin/$(CONTROL_TARGET)
	rm -f /usr/local/bin/$(GET_DATA_TARGET)
	rm -f /usr/local/bin/$(REDIS_TARGET)

# 运行目标
run-control: $(CONTROL_TARGET)
	./$(CONTROL_TARGET)

run-getdata: $(GET_DATA_TARGET)
	./$(GET_DATA_TARGET)

run-redis: $(REDIS_TARGET)
	./$(REDIS_TARGET)

run-redis-simple: $(REDIS_SIMPLE_TARGET)
	./$(REDIS_SIMPLE_TARGET)

# 调试目标
debug: CFLAGS += -DDEBUG
debug: $(CONTROL_TARGET) $(GET_DATA_TARGET) $(REDIS_TARGET)

# 发布版本
release: CFLAGS = -Wall -O2 -std=c99
release: $(CONTROL_TARGET) $(GET_DATA_TARGET) $(REDIS_TARGET)

# 帮助信息
help:
	@echo "可用的目标:"
	@echo "  all               - 编译所有模块 (默认)"
	@echo "  $(CONTROL_TARGET)        - 编译control_data模块"
	@echo "  $(GET_DATA_TARGET)       - 编译get_data_mac模块"
	@echo "  $(REDIS_TARGET)          - 编译rediscache测试模块 (需要hiredis)"
	@echo "  $(REDIS_SIMPLE_TARGET)   - 编译简单的rediscache模块 (不需要hiredis)"
	@echo "  clean             - 清理编译文件"
	@echo "  run-control       - 编译并运行control_data"
	@echo "  run-getdata       - 编译并运行get_data_mac"
	@echo "  run-redis         - 编译并运行rediscache测试 (需要hiredis)"
	@echo "  run-redis-simple  - 编译并运行简单的rediscache模块"
	@echo "  debug             - 编译调试版本"
	@echo "  release           - 编译发布版本"
	@echo "  install           - 安装到系统"
	@echo "  uninstall         - 从系统卸载"
	@echo "  help              - 显示此帮助信息"

# 声明伪目标
.PHONY: all clean install uninstall run-control run-getdata run-redis run-redis-simple debug release help
